/*
    消息队列相关类的测试
*/
#include "../../src/server/msg_queue.hpp"
#include "gtest/gtest.h"

mq::MsgQueueManager::MsgQueueManagerPtr _msg_queue_manager_ptr;
class MsgQueueTest : public testing::Environment
{ 
public:
    virtual void SetUp()
    {
        _msg_queue_manager_ptr = std::make_shared<mq::MsgQueueManager>("./data/mate.db");
    }

    virtual void TearDown()
    {
        _msg_queue_manager_ptr->clear();
    }
};

// 插入测试
TEST(exchanger_test, insert_test)
{
    google::protobuf::Map<std::string, std::string> args;
    // args.insert(google::protobuf::MapPair<std::string, std::string>("key1", "value1"));
    // args.insert(google::protobuf::MapPair<std::string, std::string>("key2", "value2"));
    // _msg_queue_manager_ptr->declareMsgQueue("queue_1", true, false, false, args);
    // _msg_queue_manager_ptr->declareMsgQueue("queue_2", true, false, false, args);
    // _msg_queue_manager_ptr->declareMsgQueue("queue_3", true, false, false, args);
    // _msg_queue_manager_ptr->declareMsgQueue("queue_4", true, false, false, args);
    ASSERT_EQ(_msg_queue_manager_ptr->size(), 4);

    // 重复插入
    bool ret = _msg_queue_manager_ptr->declareMsgQueue("queue_2", true, false, false, args);
    ASSERT_EQ(ret, true);
    ASSERT_EQ(_msg_queue_manager_ptr->size(), 4);
}

// 查找测试
TEST(exchanger_test, get_test)
{
    ASSERT_EQ(_msg_queue_manager_ptr->hasMsgQueue("queue_1"), true);
    ASSERT_EQ(_msg_queue_manager_ptr->hasMsgQueue("queue_2"), true);
    ASSERT_EQ(_msg_queue_manager_ptr->hasMsgQueue("queue_3"), true);
    ASSERT_EQ(_msg_queue_manager_ptr->hasMsgQueue("queue_4"), true);

    mq::MsgQueue::MsgQueuePtr msg_queue_ptr = _msg_queue_manager_ptr->getMsgQueue("queue_1");
    ASSERT_EQ(msg_queue_ptr->_name, "queue_1");
    ASSERT_EQ(msg_queue_ptr->_durable, true);
    ASSERT_EQ(msg_queue_ptr->_exclusive, false);
    ASSERT_EQ(msg_queue_ptr->_auto_delete, false);
    ASSERT_EQ(msg_queue_ptr->getArgs(), "key1=value1&key2=value2");
}

// 删除测试
TEST(exchanger_test, delete_test)
{
    _msg_queue_manager_ptr->removeMsgQueue("queue_2");
    ASSERT_EQ(_msg_queue_manager_ptr->size(), 3);
    ASSERT_EQ(_msg_queue_manager_ptr->hasMsgQueue("queue_2"), false);
}

int main(int argc, char **argv)
{
    testing::InitGoogleTest(&argc, argv);
    testing::AddGlobalTestEnvironment(new MsgQueueTest);
    return RUN_ALL_TESTS();
}